<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:server 指令有效
// +----------------------------------------------------------------------
return [
    // 扩展自身需要的配置
    'protocol' => 'tcp', // 协议 支持 tcp udp unix http websocket text
    'host' => '0.0.0.0', // 监听地址
    'port' => 8166, // 监听端口
    'socket' => '', // 完整监听地址
    'context' => [], // socket 上下文选项
    'worker_class' => '', // 自定义Workerman服务类名 支持数组定义多个服务

    // 支持workerman的所有配置参数
    'name' => 'thinkphp',
    'count' => 4,
    'daemonize' => false,
    'pidFile' => '',

    // 支持事件回调
    // onWorkerStart
    'onWorkerStart' => function ($worker) {
        \Workerman\Lib\Timer::add(1, function () use ($worker) {
            $redis = new \think\cache\driver\Redis();
            $a = $redis->get("wash_machine_data");

            if ($a && $worker->connections) {
                foreach ($a as $key => $item) {
                    foreach ($worker->connections as $connection) {
                        echo "正在发送的数据是:".$item.PHP_EOL;
                        $connection->send($item);
                    }
                    unset($a[$key]);
                    $redis->set("wash_machine_data", $a);
                }

            }
        });

    },
    // onWorkerReload
    'onWorkerReload' => function ($worker) {

    },
    // onConnect
    'onConnect' => function ($connection) {
        echo date("Y-m-d H:i:s", time()) . "connect success" . PHP_EOL;
    },
    // onMessage
    'onMessage' => function ($connection, $data) {
        $newData = urldecode($data);
        // echo $data;
        $newArr = unpack("H*", $newData);
        // dump($newArr);
        $hexData = $newArr[1];
        echo $hexData;
        // 获取标志符号
        $mask = hexToStr(substr($hexData, 0, 8));   // 标志符号
        $verMinor = hexdec(substr($hexData, 8, 4)); // 协议版本号 低位
        $verMajor = hexdec(substr($hexData, 12, 4));   // 协议版本号 高位
        $stationId = decNumData(substr($hexData, 16, 8));   // 设备的ID
        echo "mask:{$mask}, verMinor:{$verMinor}, verMajor:{$verMajor}" . PHP_EOL;
        echo substr($hexData, 16, 8) . PHP_EOL;   // 进位数据
        echo "洗车的车的ID" . $stationId . PHP_EOL;
        $deviceUd = decNumData(substr($hexData, 24, 4));    // 洗车点编号
        $moduleId = decNumData(substr($hexData, 28, 4));    // 设备编号
        $pwd = hexToStr(substr($hexData, 32, 32));          // 握手秘钥
        echo substr($hexData, 32, 32).PHP_EOL;
        $tPLC = decNumData(substr($hexData, 64, 8));        // PLC标记的当前的时间戳
        $fms = decNumData(substr($hexData, 72, 8));         // 机器的状态 1代表急停 2代表等待启动命令 3代表回零中 4代表回零完成 5代表执行预备过程 6代表准备好 7代表正在运行
        $waterLevel = decNumData(substr($hexData, 80, 8));  // 水位高度服务器端接收值/10  小数点一位精度，传输过来前10倍扩大在传输至云端
        $cleanerLevel = decNumData(substr($hexData, 88, 8));    // 清洁剂的液位  小数点一位精度，传输过来前10倍扩大在传输至云端
        $maskIns = decNumData(substr($hexData, 96, 8)); // 在上电和心跳包中，为 0xFFFF；在IO事件包中，发生变化的位为1
        $outs1 = decNumData(substr($hexData, 152, 8));  // 0 完成或者准备  1 喷泡沫 2 冲洗
        $carCode = substr($hexData, 160, 40);
        $carCode = unicode_to_utf8(deGDW(substr($carCode, 0, 4))).chr(decNumData(substr($carCode, 4, 4))).chr(decNumData(substr($carCode, 8, 4))).chr(decNumData(substr($carCode, 12, 4))).chr(decNumData(substr($carCode, 16, 4))).chr(decNumData(substr($carCode, 20, 4))).chr(decNumData(substr($carCode, 24, 4)));
        file_put_contents("洗车机器运作.txt", date("Y-m-d H:i:s", time())."stationId:{$stationId},deviceUd:{$deviceUd},moduleId:{$moduleId},pwd:{$pwd},tplC:{$tPLC},fms:{$fms}, waterLevel:{$waterLevel}, cleanerLevel:{$cleanerLevel}
        maskIns:{$maskIns}, carCode:{$carCode}.outs1:{$outs1}\r\n", FILE_APPEND);
        $cid = $connection->id = $deviceUd;

        echo substr($hexData, 160, 40).PHP_EOL;

        echo "stationId:{$stationId},deviceUd:{$deviceUd},moduleId:{$moduleId},pwd:{$pwd},tplC:{$tPLC},fms:{$fms}, waterLevel:{$waterLevel}, cleanerLevel:{$cleanerLevel}
        maskIns:{$maskIns}, carCode:{$carCode}.outs1:{$outs1}".PHP_EOL;
        // 更新洗车机器的信息
        if ($d = \think\facade\Db::name("store_device")->where("id", $cid)->find()) {
            $device_data = \think\facade\Db::name("store_device")->where("id", $cid)->update([
                'wash_state' => $fms,
                'water_level' => (floatval($waterLevel / 10)),
                'clean_level' => (floatval($cleanerLevel / 10)),
                'wash_car_status' => $outs1,
                'car_code' => $carCode
            ]);
            $order_car_id = $d["car_order_id"];

            echo $order_car_id."订单的ID".PHP_EOL;
            if (($outs1 == 1) || ($outs1 == 2)) {
                $order_car_id = $order_car_id ?: $d["car_order_id"];
                \think\facade\Db::name("store_order_car")->where("id", $order_car_id)->update(["status" => 1, 'car_code' => $carCode]);
                echo \think\facade\Db::name("store_order_car")->getLastSql();
            }
            if ($outs1 == 3) { // 完成
                $order_car_id = $order_car_id ?: $d["car_order_id"];
                // 更新状态
                \think\facade\Db::name("store_order_car")->where("id", $order_car_id)->update(["status" => 3, 'car_code' => $carCode]);
                \think\facade\Db::name("store_device")->where("id", $cid)->update(["car_order_id" => 0, "car_code" => 0]);
                echo \think\facade\Db::name("store_order_car")->getLastSql().PHP_EOL;
                echo \think\facade\Db::name("store_device")->getLastSql();
            }

        }
        echo \think\facade\Db::name("store_device")->getLastSql() . PHP_EOL;
        echo $hexData . PHP_EOL;

        $connection->send(255);
    },
    // onClose
    'onClose' => function ($connection) {

    },
    // onError
    'onError' => function ($connection, $code, $msg) {
        echo "error [ $code ] $msg\n";
    },
];

